package com.codemart.lanze.modules.sys.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codemart.lanze.modules.sys.mapper.DeptMapper;
import com.codemart.lanze.modules.sys.model.Dept;
import com.codemart.lanze.modules.sys.vo.TreeNode;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * @description:
 * @author: lanze
 */
@Transactional
@Service
public class DeptService extends ServiceImpl<DeptMapper, Dept> {

	@Resource
	private DeptMapper deptMapper;

	@Transactional(readOnly = true)
	public List<TreeNode> findTreeList() {
		return deptMapper.findTreeList();
	}

	@Transactional(readOnly = true)
	public IPage<Dept> findPage(Integer pageNum, Integer pageSize, Long deptId, String name, String startTime,
			String endTime) {
		QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();

		if (null != deptId) {
			queryWrapper.eq("parentId", deptId);
		}
		if (StrUtil.isNotEmpty(name)) {
			queryWrapper.like("name", name);
		}
		if (StrUtil.isNotEmpty(startTime)) {
			queryWrapper.ge("createTime", DateUtil.beginOfDay(DateUtil.parse(startTime)));
		}
		if (StrUtil.isNotEmpty(endTime)) {
			queryWrapper.le("createTime", DateUtil.endOfDay(DateUtil.parse(endTime)));
		}

		// 排序
		queryWrapper.orderByDesc("sort");

		IPage<Dept> iPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);

		return iPage;
	}

	@Transactional(readOnly = true)
	public String getDeptIdList(Long deptId) {

		return null;
	}
}
